<!-- src/views/LoginPage.vue -->
<template>
  <div class="login-container">
    <div class="login-form">
      <h1>Login</h1>
      <form @submit.prevent="handleLogin">
        <input type="email" v-model="loginData.loginEmail" placeholder="Email" required />
        <input type="password" v-model="loginData.password" placeholder="Password" required />
        <button type="submit">Login</button>
      </form>
    </div>
  </div>
</template>


<script>
import axios from 'axios';

const apiClient = axios.create({
  baseURL: process.env.VUE_APP_API_BASE_URL,
  timeout: 5000,
});

export default {
  name: 'LoginPage',
  data() {
    return {
      loginData: {
        loginEmail: '',
        password: '',
        loginTarget: 'Staff_Entrance',
      },
    };
  },
  methods: {
    async handleLogin() {
      try {
        const response = await apiClient.post('/public/auth/user/login/email', this.loginData);
        if (response.data.code === 204) {
          this.$store.dispatch('loginSuccess', response.data.result);
          // 登录成功，存储token和其他用户信息
          // localStorage.setItem('user', JSON.stringify(response.data.result));
          // localStorage.setItem('staffInfo', JSON.stringify(response.data.result.staff));
          // localStorage.setItem('token', JSON.stringify(response.data.result.token));
          // 跳转到主页或其他页面
          this.$router.push('/');
        } else {
          // 登录失败，显示错误信息
          alert(response.data.msg);
        }
      } catch (error) {
        console.error('登录失败:', error);
        alert('登录失败，请检查网络连接或联系管理员');
      }
    },
  },
};
</script>
  
<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('@/assets/login_bg.jpg') no-repeat center center fixed;
  background-size: cover;
}

.login-form {
  background: rgba(255, 255, 255, 0.8);
  padding: 20px;
  border-radius: 5px;
  text-align: center;
  width: 300px;
}

.login-form h1 {
  margin-top: 0;
}

.login-form input[type="email"],
.login-form input[type="password"],
.login-form button {
  width: 100%;
  padding: 10px;
  margin: 10px 0;
  border: 1px solid #ccc;
  border-radius: 5px;
}

.login-form button {
  background-color: #007BFF;
  color: white;
  border: none;
  cursor: pointer;
}

.login-form button:hover {
  background-color: #0056b3;
}
</style>
